
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>使用C语言获取linux系统相关信息 | 安生</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="最近在写shell的时候，涉及到了获取环境变量参数和本地计算机相关信息，包括计算机设备名，用户名的信息，在这里简单总结一下。
获取环境变量各项参数，可以直接使用getenv函数。man中关于getenv函数的声明如下：
12#include &amp;amp;lt;stdlib.h&amp;amp;gt;char *getenv(const char *name);
函数中，参数name 为环境变量的名称, 如果">
<meta property="og:type" content="article">
<meta property="og:title" content="使用C语言获取linux系统相关信息">
<meta property="og:url" content="http://localhost/2014/07/24/使用C语言获取linux系统相关信息/index.html">
<meta property="og:site_name" content="安生">
<meta property="og:description" content="最近在写shell的时候，涉及到了获取环境变量参数和本地计算机相关信息，包括计算机设备名，用户名的信息，在这里简单总结一下。
获取环境变量各项参数，可以直接使用getenv函数。man中关于getenv函数的声明如下：
12#include &amp;amp;lt;stdlib.h&amp;amp;gt;char *getenv(const char *name);
函数中，参数name 为环境变量的名称, 如果">
<meta property="og:updated_time" content="2016-04-11T06:32:40.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用C语言获取linux系统相关信息">
<meta name="twitter:description" content="最近在写shell的时候，涉及到了获取环境变量参数和本地计算机相关信息，包括计算机设备名，用户名的信息，在这里简单总结一下。
获取环境变量各项参数，可以直接使用getenv函数。man中关于getenv函数的声明如下：
12#include &amp;amp;lt;stdlib.h&amp;amp;gt;char *getenv(const char *name);
函数中，参数name 为环境变量的名称, 如果">
  
    <link rel="alternative" href="/atom.xml" title="安生" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  <link rel="stylesheet" href="/css/style.css">
  <link href="//fonts.useso.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <!--[if lt IE 9]><script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7/html5shiv.min.js"></script><![endif]-->
  
</head>
<body>
<div id="container">
  <div id="wrap">
    <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">安生</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="http://www.baidu.com/baidu" method="get" accept-charset="utf-8" class="search-form">
          <input type="search" name="word" maxlength="20" class="search-form-input" placeholder="Search">
          <input type="submit" value="" class="search-form-submit">
          <input name=tn type=hidden value="bds">
          <input name=cl type=hidden value="3">
          <input name=ct type=hidden value="2097152">
          <input type="hidden" name="si" value="localhost">
        </form>
      </div>
    </div>
  </div>
</header>
    <div class="outer">
      <section id="main"><article id="post-使用C语言获取linux系统相关信息" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2014/07/24/使用C语言获取linux系统相关信息/" class="article-date">
  <time datetime="2014-07-24T09:54:00.000Z" itemprop="datePublished">2014-07-24</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      使用C语言获取linux系统相关信息
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>最近在写shell的时候，涉及到了获取环境变量参数和本地计算机相关信息，包括计算机设备名，用户名的信息，在这里简单总结一下。</p>
<p>获取环境变量各项参数，可以直接使用getenv函数。man中关于getenv函数的声明如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> &amp;lt;stdlib.h&amp;gt;</span></span><br><span class="line"><span class="function"><span class="keyword">char</span> *<span class="title">getenv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *name)</span></span>;</span><br></pre></td></tr></table></figure>
<p>函数中，参数name 为环境变量的名称, 如果该变量存在则会返回指向该内容的指针. 环境变量的格式为name＝value。执行成功则返回指向该内容的指针, 找不到符合的环境变量名称则返回NULL。</p>
<p>这里我使用环境变量获取用户名称和当前目录为例，首先我们知道在环境变量中，用户名保存在“LOGNAME”中，而当前目录保存在&quot;PWD&quot;中，所以函数调用应该为</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">getenv(“LOGNAME”)</span><br><span class="line">getenv(“PWD”);</span><br></pre></td></tr></table></figure>
<p>所以，简单的测试程序就出来了，程序源码如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc,<span class="keyword">char</span> **argv)</span></span><br><span class="line"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> *name;</span><br><span class="line">    <span class="keyword">char</span> *path;</span><br><span class="line"></span><br><span class="line">    name = getenv(<span class="string">"LOGNAME"</span>);</span><br><span class="line">    path = getenv(<span class="string">"PWD"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"logname = %s\n"</span>,name);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"pwd = %s\n"</span>,path);</span><br><span class="line"></span><br><span class="line">    return <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>测试程序没有包含错误处理，如有需要，请自行加上。在我的电脑上运行如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">wangbo@iDina-PC:~/linuxc/tmp$ ./a.out</span><br><span class="line">logname = wangbo</span><br><span class="line">pwd = /home/wangbo/linuxc/tmp</span><br></pre></td></tr></table></figure>
<p>但是呢，我们试着请通过环境变量获取系统设备名称，是无法获取的。原因我也不太清楚，如果知道的话，赶快告诉我吧。</p>
<p>不能从环境变量获取设备名，就只能从其他地方获取了。恩，这里给大家介绍一个函数，gethostname函数，可以直接获取系统设备名。</p>
<p>声明如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">gethostname</span><span class="params">(<span class="keyword">char</span> *name, size_t len)</span></span>;</span><br></pre></td></tr></table></figure>
<p>函数中，name是指向你所设置的保存设备名的字符串的指针，而len是name的大小，成功的话返回0,获取失败，返回-1,错误代码保存在errno中。</p>
<p>函数比较简单，这里直接给出测试代码：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> **argv)</span></span><br><span class="line"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> hostname[<span class="number">32</span>];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>( gethostname(hostname,sizeof(hostname)) )</span><br><span class="line">    &#123;</span><br><span class="line">        perror(<span class="string">"gethostname"</span>);</span><br><span class="line">        return (<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"localhost name:%s\n"</span>,hostname);</span><br><span class="line"></span><br><span class="line">    return <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在我的电脑上运行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">wangbo@iDina-PC:~/linuxc/tmp$ ./a.out</span><br><span class="line">localhost name:iDina-PC</span><br></pre></td></tr></table></figure>
<p>后来查询相关资料，又发现一个函数，uname，可以获取系统包括版本在内的相关信息，函数声明如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/utsname.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">uname</span><span class="params">(<span class="keyword">struct</span> utsname *buf)</span></span>;</span><br></pre></td></tr></table></figure>
<p>struct utsname 是一个系统定义好的结构体，结构体定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> utsname&#123;</span><br><span class="line">    <span class="keyword">char</span> sysname[];       <span class="comment">/* 操作系统名称 (e.g., "Linux") */</span></span><br><span class="line">    <span class="keyword">char</span> nodename[];      <span class="comment">/* 当前设备在网络上的名称 (e.g,"localhost.localdomain")*/</span></span><br><span class="line">    <span class="keyword">char</span> release[];       <span class="comment">/* 系统发布级别(e.g., "2.6.28") */</span></span><br><span class="line">    <span class="keyword">char</span> version[];       <span class="comment">/* 系统发布版本 */</span></span><br><span class="line">    <span class="keyword">char</span> machine[];       <span class="comment">/* 当前设备硬件体系(e.g.,"i686") */</span></span><br><span class="line">    <span class="meta">#<span class="meta-keyword">ifdef</span> _GNU_SOURCE</span></span><br><span class="line">    <span class="keyword">char</span> domainname[];    <span class="comment">/* NIS或者YP域名 */</span></span><br><span class="line">    <span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>函数正确返回0,错误返回-1.错误代码保存在errno中。</p>
<p>和上面一样，比较简单，测试代码如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/utsname.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc,<span class="keyword">char</span> **argv)</span></span><br><span class="line"></span>&#123;</span><br><span class="line">    <span class="keyword">struct</span> utsname buf;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(uname(&amp;buf))</span><br><span class="line">    &#123;</span><br><span class="line">        perror(<span class="string">"uname"</span>);</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"sysname:%s\n"</span>,buf.sysname);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"nodename:%s\n"</span>,buf.nodename);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"release:%s\n"</span>,buf.release);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"version:%s\n"</span>,buf.version);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"machine:%s\n"</span>,buf.machine);</span><br><span class="line"></span><br><span class="line">    return <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我的电脑上运行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">wangbo@iDina-PC:~/linuxc/tmp$ ./a.out </span><br><span class="line">sysname:Linux</span><br><span class="line">nodename:iDina-PC</span><br><span class="line">release:3.13.0-32-generic</span><br><span class="line">version:#57-Ubuntu SMP Tue Jul 15 03:51:12 UTC 2014</span><br><span class="line">machine:i686</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://localhost/2014/07/24/使用C语言获取linux系统相关信息/" data-id="cimvrqg1z0005kyfe0ghzbpfk" class="article-share-link">分享到</a>
      

      
        <a href="http://localhost/2014/07/24/使用C语言获取linux系统相关信息/#ds-thread" class="article-comment-link">评论</a>
      

      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2014/11/17/CDN，网站加速利器/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">
        
          CDN，网站加速利器
        
      </div>
    </a>
  
  
    <a href="/2014/07/18/如何使用getopt-函数解析参数/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">如何使用getopt()函数解析参数</div>
    </a>
  
</nav>

  
</article>


  <section id="comments">
    <div id="ds-thread" class="ds-thread" data-thread-key="2014/07/24/使用C语言获取linux系统相关信息/" data-title="使用C语言获取linux系统相关信息" data-url="http://localhost/2014/07/24/使用C语言获取linux系统相关信息/"></div>
  </section>
</section>
      
      <aside id="sidebar">
  
    
  
    
  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/">五月 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/04/">四月 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/12/">十二月 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/11/">十一月 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/07/">七月 2014</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/06/">六月 2014</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">近期文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2015/05/01/Linux服务器配置与LNMP安装实录-1/">Linux服务器配置与LNMP安装实录---(1)</a>
          </li>
        
          <li>
            <a href="/2015/04/16/科学上网，简易教程/">科学上网，简易教程</a>
          </li>
        
          <li>
            <a href="/2014/12/17/记Fedora-21-Yun源配置和软件安装/">记Fedora 21 Yun源配置和软件安装</a>
          </li>
        
          <li>
            <a href="/2014/12/09/Linux运维文档之nginx【转】/">Linux运维文档之nginx【转】</a>
          </li>
        
          <li>
            <a href="/2014/11/26/CentOS-7安装完第一步：yum源配置/">CentOS 7安装完第一步：yum源配置</a>
          </li>
        
      </ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">友情链接</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="http://xiguabaobao.com" target="_blank">主题作者</a>
          </li>
        
          <li>
            <a href="http://reqianduan.com" target="_blank">热前端</a>
          </li>
        
          <li>
            <a href="https://www.xiyoulinux.org" target="_blank">西邮linux兴趣小组</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
      
    </div>
    <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2016 wangbo<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
      .
      Theme by <a href="https://github.com/xiangming/landscape-plus" target="_blank">Landscape-plus</a>
    </div>
  </div>
</footer>
  </div>
  <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
  <!-- totop start -->
<div id="totop">
<a title="返回顶部"><img src="/img/scrollup.png"/></a>
</div>

<!-- totop end -->

<!-- 多说公共js代码 start -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"idina"};
  (function() {
    var ds = document.createElement('script');
    ds.type = 'text/javascript';ds.async = true;
    ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
    ds.charset = 'UTF-8';
    (document.getElementsByTagName('head')[0]
     || document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
  </script>
<!-- 多说公共js代码 end -->


<!-- 百度分享 start -->

<!-- 百度分享 end -->

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>




<script src="/js/script.js"></script>

</div>
</body>
</html>
